﻿<template>
	<div class="device-container">
		<el-dialog v-model="isShowDialog" :width="900" draggable="" :close-on-click-modal="false">
			<template #header>
				<div style="color: #fff">
					<span>{{ props.title }}</span>
				</div>
			</template>
			<el-form :model="upParams" ref="ruleFormRef" label-width="110" :rules="rules">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" class="mb20">
						<el-form-item label="活体检测等级" prop="name">
							<el-input-number v-model="upParams.face.alive_level" size="small" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="16" :md="16" :lg="16" :xl="16" class="mb20">
						<el-form-item label="说明" prop="clientSn"> 活体检测等级 1:弱等级 2:强等级 </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" class="mb20">
						<el-form-item label="重复超时" prop="name">
							<el-input-number v-model="upParams.face.derep_timeout" size="small" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="16" :md="16" :lg="16" :xl="16" class="mb20">
						<el-form-item label="说明" prop="clientSn"> 重复超时。取值范围：3~60。（去除重复开关为true时存在） </el-form-item>
					</el-col>

					<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" class="mb20">
						<el-form-item label="确信分数" prop="name">
							<el-input-number v-model="upParams.face.match_score" size="small" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="16" :md="16" :lg="16" :xl="16" class="mb20">
						<el-form-item label="说明" prop="clientSn"> 确信分数。取值范围：70~100。（比对开关为true时存在） </el-form-item>
					</el-col>

					<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" class="mb20">
						<el-form-item label="输出图像质量" prop="name">
							<el-input-number v-model="upParams.face.quality" size="small" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="16" :md="16" :lg="16" :xl="16" class="mb20">
						<el-form-item label="说明" prop="clientSn"> 输出图像质量。取值范围：0~100 </el-form-item>
					</el-col>

					<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" class="mb20">
						<el-form-item label="活体检测开关" prop="name">
							<el-switch v-model="upParams.face.enable_alive" active-text="是" inactive-text="否" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="16" :md="16" :lg="16" :xl="16" class="mb20">
						<el-form-item label="说明" prop="clientSn"> 活体检测开关 </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" class="mb20">
						<el-form-item label="去除重复开关" prop="name">
							<el-switch v-model="upParams.face.enable_dereplication" active-text="是" inactive-text="否" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="16" :md="16" :lg="16" :xl="16" class="mb20">
						<el-form-item label="说明" prop="clientSn"> 去除重复开关。true：开 false：关。 </el-form-item>
					</el-col>
					<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" class="mb20">
						<el-form-item label="比对开关" prop="name">
							<el-switch v-model="upParams.face.enable_match" active-text="是" inactive-text="否" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="16" :md="16" :lg="16" :xl="16" class="mb20">
						<el-form-item label="说明" prop="clientSn"> 比对开关。true：开 false：关。 </el-form-item>
					</el-col>

					<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" class="mb20">
						<el-form-item label="输出失败图像" prop="name">
							<el-switch v-model="upParams.face.output_not_matched" active-text="是" inactive-text="否" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="16" :md="16" :lg="16" :xl="16" class="mb20">
						<el-form-item label="说明" prop="clientSn"> 是否输出对比失败图像,输出不匹配人脸。true：输出 false：不输出。 </el-form-item>
					</el-col>

					<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" class="mb20">
						<el-form-item label="重复注册" prop="name">
							<el-switch v-model="upParams.face.enable_same_face_reg" active-text="是" inactive-text="否" />
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="16" :md="16" :lg="16" :xl="16" class="mb20">
						<el-form-item label="说明" prop="clientSn">是否允许相同人脸重复注册 </el-form-item>
					</el-col>

					<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" class="mb20">
						<el-form-item label="图像输出形式" prop="name">
							<el-select v-model="upParams.face.pic_form" placeholder="Select" size="small" style="width: 240px">
								<el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="16" :md="16" :lg="16" :xl="16" class="mb20">
						<el-form-item label="说明" prop="clientSn">overall：全景 closeup：特写 both：全景+特写 none：无图。 </el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="cancel">取 消</el-button>
					<el-button type="primary" @click="submit">确 定</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>
<style lang="scss" scoped>
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>
<script lang="ts" setup>
import { ref, onMounted } from 'vue';
import { ElMessage } from 'element-plus';
import type { FormRules } from 'element-plus';
import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
import { getDictLabelByVal as dv } from '/@/utils/dict-utils';
import { formatDate } from '/@/utils/formatTime';
import { addDevice, updateDevice, detailDevice } from '/@/api/main/device';
import { getProjectProjectIdDropdown } from '/@/api/main/device';
import { getAPI } from '/@/utils/axios-utils';
import { SysEnumApi } from '/@/api-services/api';
import request from '/@/utils/request';
import { ElMessageBox } from 'element-plus';
import type { Action } from 'element-plus';

//父级传递来的参数
var props = defineProps({
	title: {
		type: String,
		default: '',
	},
});
//父级传递来的函数，用于回调
const emit = defineEmits(['reloadTable']);
// 定义 FaceParams 接口
interface FaceParams {
	alive_level: number; // 活体检测等级
	derep_timeout: number; // 重复超时
	enable_alive: boolean; // 活体检测开关
	enable_dereplication: boolean; // 去除重复开关
	enable_match: boolean; // 比对开关
	enable_same_face_reg: boolean; // 是否允许相同人脸重复注册
	match_score: number; // 确信分数
	pic_form: string; // 图像输出形式
	output_not_matched: boolean; // 是否输出对比失败图像
	quality: number; // 输出图像质量
}

// 定义 UpdateAppParamsRequest 接口
interface UpdateAppParamsRequest {
	DeviceSn: string; // 设备序列号
	face: FaceParams; // 面部参数
}
// 初始化 upParams
const upParams = ref<UpdateAppParamsRequest>({
	DeviceSn: '',
	face: {
		alive_level: 1,
		derep_timeout: 3,
		enable_alive: false,
		enable_dereplication: true,
		enable_match: true,
		enable_same_face_reg: true,
		match_score: 80,
		pic_form: 'closeup',
		output_not_matched: true,
		quality: 70,
	},
});
const options = [
	{
		value: 'overall',
		label: '全景',
	},
	{
		value: 'closeup',
		label: '特写',
	},
	{
		value: 'both',
		label: '全景+特写',
	},
	{
		value: 'none',
		label: '无图',
	},
];
const loading = ref(false);
const ruleFormRef = ref();
const isShowDialog = ref(false);
const ruleForm = ref<any>({});
//自行添加其他规则
const rules = ref<FormRules>({
	name: [{ required: true, message: '请输入名称！', trigger: 'blur' }],
	clientSn: [{ required: true, message: '请输入客户端设备号！', trigger: 'blur' }],
});

// 页面加载时
onMounted(() => {});

// 打开弹窗
const openDialog = async (row: any) => {
	// ruleForm.value = JSON.parse(JSON.stringify(row));
	// 改用detail获取最新数据来编辑
	let rowData = JSON.parse(JSON.stringify(row));
	if (rowData.id) ruleForm.value = (await detailDevice(rowData.id)).data.result;
	else ruleForm.value = rowData;
	isShowDialog.value = true;
};

// 关闭弹窗
const closeDialog = () => {
	emit('reloadTable');
	isShowDialog.value = false;
};

// 取消
const cancel = () => {
	isShowDialog.value = false;
};

// 提交
const submit = async () => {
	// console.log(upParams.value);
	upParams.value.DeviceSn = ruleForm.value.clientSn;
	loading.value = true;
	var res = await request({
		url: '/api/device/cmd_UpdateAppParams',
		method: 'post',
		data: upParams.value,
	});
	// 'success' | 'info' | 'warning' | 'error'
	if (res.data.code == 200 && res.data.result.code == 0) {
		ElMessageBox.alert(res.data.message + res.data.result.msg + '结果为:' + res.data.result.result, '获取应用参数', {
			confirmButtonText: '已知晓',
			type: 'success',
			callback: (action: Action) => {
				loading.value = false;
				closeDialog();
			},
		});
	} else {
		ElMessageBox.alert(res.data.message + res.data.result.msg, '获取应用参数-失败', {
			confirmButtonText: '已知晓',
			type: 'error',
			callback: (action: Action) => {
				loading.value = false;
				closeDialog();
			},
		});
	}
};

const projectProjectIdDropdownList = ref<any>([]);
const getProjectProjectIdDropdownList = async () => {
	let list = await getProjectProjectIdDropdown();
	projectProjectIdDropdownList.value = list.data.result ?? [];
};
getProjectProjectIdDropdownList();

//将属性或者函数暴露给父组件
defineExpose({ openDialog });
</script>
